import java.awt.*;

import javax.swing.*;

/**
 * @author
 * 
 */
public class View {
	Resource[] resources;
	Ticker ticks;
	Decal[] decals;
	Item[] items;
	JFrame frame;
	JLayeredPane layeredPane;
	String[] map;
	ItemsLayer l_items;
	UnitsLayer l_units;
	BuildingsLayer l_buildings;
	StatsLayer l_stats;
	TerrainLayer l_terrain;
	CursorLayer l_cursor;
	Cursor cursor;
	Player[] players;
	StructureOverview l_structOverview;
	UnitOverview l_unitOverview;

	public View(Cursor cursor, String[] map, Decal[] decals, Resource[] resources, Item[] items,
			KeyboardListener KL, Player[] players, Ticker ticks) {
		this.resources=resources;
		this.ticks=ticks;
		this.items = items;
		this.decals = decals;
		this.cursor = cursor;
		this.map = map;
		this.players = players;

		// Create a layered pane
		layeredPane = new JLayeredPane();
		layeredPane.setPreferredSize(new Dimension(1000, 550));
		layeredPane.setLayout(new FlowLayout());

		// Create areaView layers
		l_terrain = new TerrainLayer(map, decals,resources, cursor);
		l_units = new UnitsLayer(cursor, players);
		l_buildings = new BuildingsLayer(cursor);
		l_cursor = new CursorLayer(cursor);
		l_items = new ItemsLayer(cursor,items);
		
		// Create statusView layer
		l_stats = new StatsLayer(cursor,ticks,players[0]);

		// Create Structure Overview layer
		l_structOverview = new StructureOverview(players[0], KL, this);

		// Create Unit Overview layer
		l_unitOverview = new UnitOverview(players[0], KL, this);

		// Combine layers
		layeredPane.add(l_unitOverview, new Integer(7));
		l_unitOverview.setEnabled(false);
		l_unitOverview.setFocusable(false);
		l_unitOverview.setVisible(false);
		layeredPane.add(l_structOverview, new Integer(6));
		l_structOverview.setEnabled(false);
		l_structOverview.setFocusable(false);
		l_structOverview.setVisible(false);

		layeredPane.add(l_cursor, new Integer(5));
		layeredPane.add(l_units, new Integer(4));
		layeredPane.add(l_stats, new Integer(3));
		layeredPane.add(l_items, new Integer(2));
		layeredPane.add(l_buildings, new Integer(1));
		layeredPane.add(l_terrain, new Integer(0));

		l_units.setOpaque(false);
		l_buildings.setOpaque(false);
		l_terrain.setOpaque(false);
		l_cursor.setOpaque(false);
		l_items.setOpaque(false);
		
		// Create frame
		frame = new JFrame("");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// add KeyListener
		frame.addKeyListener(KL);

		// Add layered pane to the frame and display the frame
		frame.setContentPane(layeredPane);
		frame.pack();
		frame.setVisible(true);
	}

	public void refresh(int mode) {
		if (mode == 0) {
			l_terrain.repaint();
			l_buildings.repaint();
			l_units.repaint();
			l_stats.repaint();
			l_cursor.repaint();
		} else if (mode == 1) {
			l_stats.repaint();
			l_cursor.repaint();
		} else if (mode == 2) {
			l_unitOverview.refresh();
		} else if (mode == 3)
			l_structOverview.refresh();
		{
			// layeredPane.add(l_unitOverview, new Integer(6));
			// layeredPane.add(l_structOverview, new Integer(5));

		}

	}

	public void setFocus(int index) {
		if (index == 0) {
			frame.enableInputMethods(true);
			frame.requestFocus();
		} else if (index == 1) {
			l_unitOverview.setEnabled(true);
			l_unitOverview.setFocusable(true);
			l_unitOverview.setVisible(true);
			l_unitOverview.enableInputMethods(true);
			l_unitOverview.table.requestFocus();
		} else if (index == 2) {
			l_structOverview.setEnabled(true);
			l_structOverview.setFocusable(true);
			l_structOverview.setVisible(true);
			l_structOverview.enableInputMethods(true);
			l_structOverview.table.requestFocus();
		}

	}

}